using System;
using System.Collections.Generic;
using System.Text;

namespace WGT
{
    public partial class Dialog
    {
        public void Repeater<T>(
            pos p, 
            Func<List<T>> get, 
            Func<string> hTempl, 
            Func<T, string> iTempl, 
            Func<string> fTempl, string ctx)
        {
            int id = 0;
            pos op = null;
            string octx = null;

            GetPut(ref op, ref p);
            GetPut(ref octx, ref ctx);

            if (mode == MODE.SHOW)
            {
                id = iNextId++;
                RenderRepeater(p, hTempl, get(), iTempl, fTempl, id.ToString());
            }

            GetPut(ref id, ref id);
            string sid = id.ToString();

            if (mode == MODE.UPDATE)
            {
                if (octx != ctx)
                {
                    E(sid);
                    RenderRepeater(p, hTempl, get(), iTempl, fTempl, id.ToString());
                }
            }

            else if (mode == MODE.ERASE)
            {
                E(sid);
            }

            if (mode != MODE.ERASE)
            {
                if (bHorizontal) gx += p.w;
                else gy += p.h;
            }

        }

        private void RenderRepeater<T>(pos p, Func<string> hTempl, List<T> items, Func<T, string> iTempl, Func<string> fTempl, string id)
        {
            if (items.Count > 0)
            {
                var html = new StringBuilder("<div id='{0}'>");
                html.Append(hTempl());
                items.ForEach(x => html.Append(iTempl(x)));
                html.Append(fTempl());
                html.Append("</div>");

                S(html.ToString(), id);
                U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
            }
        }
    }
}